{extend name="public:layout" /}
{block name="title"}库存预警列表{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <li><i class="fa fa-hand-o-right"></i> 库存预警列表</li>
            </ol>
        </div>
        <div class="ibox-content">
            <div class="alert alert-danger" role="alert">若要对某件商品进行补货，请先选中其左侧的复选框，然后再点“批量设置库存”按钮。</div>
            <div class="toolbar">
                <div class="pull-left">
                    <button type="button" class="btn btn-white" onclick="CheckAll();">全</button>
                    <button type="button" class="btn btn-white" onclick="CheckOther();">反</button>
                    <button type="button" class="btn btn-primary" onclick="batchUpdateStock();">批量设置库存</button>
                </div>
                <div class="pull-right">
                    <form class="form-inline" method="post">
                        <div class="form-group">
                            <div class="input-group">
                                <input class="form-control" name="goods_name" type="text" value="{$query.goods_name}" placeholder="请输入商品名称">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-info">
                            <i class="fa fa-search"></i> 搜索
                        </button>
                        <a class="btn btn-white" href="{:url('index')}">
                            <i class="fa fa-trash"></i> 清空
                        </a>
                    </form>
                </div>
                <div class="clearfix"></div>
            </div>
            <form method="post" class="form-horizontal" id="form">
            <table class="table table-bordered" id="grid">
                <thead>
                <tr>
                    <th width="50">#</th>
                    <th>商品名称</th>
                    <th width="120">库存数量</th>
                    <th width="120">预警数量</th>
                    <th width="120">进货量</th>
                    <th width="120">操作</th>
                </tr>
                </thead>
                <tbody>
                {volist name="list" id="vo"}
                <tr>
                    <td>
                        <input type="checkbox" name="stock[{$vo.sku_id}][sku_id]" value="{$vo.sku_id}" />
                    </td>
                    <td>
                        <img src="{$vo.goods_thumb|default='/public/nopic.gif'}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                        {$vo.goods_name} {$vo.sku_name}
                    </td>
                    <td>
                        <input type="hidden" class="form-control" name="stock[{$vo.sku_id}][goods_stock]" value="{$vo.goods_stock}" />
                        {$vo.goods_stock}
                    </td>
                    <td>{$vo.alert_num}</td>
                    <td>
                        <input type="number" class="form-control stockpiling" name="stock[{$vo.sku_id}][stockpiling]" value="100" />
                    </td>
                    <td>
                        <a href="javascript:;" onclick="updateStock(this);"
                           data-url="{:url('updateStock')}"
                           data-skuid="{$vo.sku_id}"
                           data-goodsstock="{$vo.goods_stock}">设置库存</a>
                    </td>
                </tr>
                {/volist}
                </tbody>
            </table>
            </form>
            <div class="page-bar">{$list->render()}</div>
            <div class="clearfix"></div>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script>
    //设置单个库存
    function updateStock(obj) {
        var url = $(obj).data('url');
        var skuId = $(obj).data('skuid');
        var goodsStock = $(obj).data('goodsstock');
        var stockpiling = $(obj).closest('tr').find('.stockpiling').val();
        getAjax(url,{sku_id:skuId,goods_stock:goodsStock,stockpiling:stockpiling},function (res) {
            if(res.code==1){
                rePage();
            }else{
                layer.alert(res.msg);
            }
        })
    }
    function batchUpdateStock() {
        var skuIds = CheckboxValue();
        var params = $("#form").serialize();
        if(IsMore(skuIds)){
            getAjax("{:url('batchUpdateStock')}",params,function (res) {
                if(res.code==1){
                    rePage();
                }else{
                    layer.alert(res.msg);
                }
            })
        }
    }
</script>
{/block}